package com.e3.basdoc.currency.dao;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.currency.entity.CurrencyEntity;
import com.e3.basdoc.currency.model.CurrencyQC;
import com.e3.core.base.SuperDAO;

public class CurrencyDAO extends SuperDAO {
	
	private String selectSQL = "SELECT t FROM CurrencyEntity t";
	private String countSQL = "SELECT count(t) FROM CurrencyEntity t";
	
	@SuppressWarnings("rawtypes")
	public List<Long> getCurrencyIdsByCondition(CurrencyQC qc) {
		String selectSQL = "SELECT t.id FROM CurrencyEntity t";
		qc.setPrefix("t");
		String whereSQL = qc.getWhereSQL();
		if (whereSQL != null && whereSQL.length() > 0) {
			selectSQL = selectSQL + " WHERE " + whereSQL;
		}
		List result = executeHQLQuery(selectSQL, null);
		if(result!=null){
			List<Long> ids = new ArrayList<Long>(result.size());
			for (Object object : result) {
				ids.add((Long)object);
			}
			return ids;
		}
		return null;
	}

	public List<CurrencyEntity> getAllCurrencys(){
		return this.find(selectSQL);
	}

	public CurrencyEntity getCurrencyByCode(String code) {
		String whSql = " WHERE t.code = ?";
		String sql = selectSQL + whSql;
		List<CurrencyEntity> entities = this.find(sql, code);
		return (entities == null || entities.size() == 0) ? null : entities.get(0);
	}
	
	public void deleteCurrencyByIds(List<Long> ids) {
		if(ids == null||ids.isEmpty()){
			return;
		}
		
		Long[] idArray = new Long[ids.size()];
		ids.toArray(idArray);
		this.deleteAllBySingleField(CurrencyEntity.class, "id", idArray);
	}
	
	public long getCurrencySizeByCode(String code){
		String whSql = " WHERE t.code = ?";
		String sql = countSQL + whSql;
		return counter(sql,code);
	}
	
	public long getCurrencySizeByName(String name){
		String whSql = " WHERE t.name = ?";
		String sql = countSQL + whSql;
		return counter(sql,name);
	}

}
